package com.iflytek.autofly.setting.biz.navi;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemProperties;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.iflytek.autofly.navisetting.NaviSetting;
import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.base.BaseFragment;
import com.iflytek.autofly.setting.biz.base.CommonRadioButtonBean;
import com.iflytek.autofly.setting.biz.base.CommonRadioGroupAdpt;
import com.iflytek.autofly.setting.widget.InnerListView;
import com.iflytek.autofly.utils.FlyLog;
import com.iflytek.sunflower.FlowerCollector;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>Description: </b>TODO<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/11/21</td>
 * <td>22:22</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class NaviFragment extends BaseFragment {
    protected static final String TAG = "NaviFragment";
    protected static final String PROPERTY_MAPAUTOMODE = "persist.sys.autofly.navishow";

    private static final String[] CMD = {"autobrightness", "brightness"};

    private View mView;
    private TextView mConentTv;
    private TextView mContentDesTv;
    private InnerListView mContentLv;
    private TextView mMapShowTv;
    private TextView mMapShowDesTv;
    private InnerListView mMapShowLv;
    private TextView mNaviModeTv;
    private TextView mNaviModeDesTv;
    private InnerListView mNaviModeLv;

    private NaviSetting mNaviSetting;

    private AtomicInteger mServiceReady = new AtomicInteger(0);

    private volatile NaviSetting.AidlStatus mAidlStatus = NaviSetting.AidlStatus.SERVICE_AIDL_NOT_BOUND;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNaviSetting = NaviSetting.registerListener(getActivity(), new NaviSetting.IBindListener() {
            @Override
            public void onServiceStatusChanged(NaviSetting.AidlStatus status) {
                FlyLog.d(TAG, "status: " + status);
                mAidlStatus = status;
                if (mAidlStatus == NaviSetting.AidlStatus.SERVICE_AIDL_BOUND) {
                    renew();
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        FlyLog.d("trace");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mView = view;
        init(view);
        return view;
    }

    @Override
    public void onDestroy() {
        FlyLog.d(TAG, "destory client");
        mNaviSetting.destoryClient(getActivity());
        mServiceReady.set(0);
        super.onDestroy();
    }

    private void init(View v) {
        addUnit(R.layout.navicontent, R.layout.navicontent_sub, new OnExpandChangeListener() {
            @Override
            public void onExpandChanged(boolean isExpanded) {
                mContentDesTv.setVisibility(!isExpanded ? View.VISIBLE : View.GONE);
                mConentTv.setVisibility(!isExpanded ? View.VISIBLE : View.GONE);
            }
        }, null);
        addUnit(R.layout.navimapshow, R.layout.navimapshow_sub, new OnExpandChangeListener() {
            @Override
            public void onExpandChanged(boolean isExpanded) {
                mMapShowDesTv.setVisibility(!isExpanded ? View.VISIBLE : View.GONE);
                mMapShowTv.setVisibility(!isExpanded ? View.VISIBLE : View.GONE);
            }
        }, null);
        addUnit(R.layout.navimode, R.layout.navimode_sub, new OnExpandChangeListener() {
            @Override
            public void onExpandChanged(boolean isExpanded) {
                mNaviModeDesTv.setVisibility(!isExpanded ? View.VISIBLE : View.GONE);
                mNaviModeTv.setVisibility(!isExpanded ? View.VISIBLE : View.GONE);
            }
        }, null);
        addHiddenUnit(R.layout.aboutsystem);

        initContentView(v);
        initMapShowView(v);
        initNaviModeView(v);
    }

    @Override
    public void renew() {
        if (mServiceReady.incrementAndGet() < 3) {
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                final boolean playState = mNaviSetting.getContentPlayState();
                final NaviSetting.PlayedContentLevel level = mNaviSetting.getPlayedContentLevel();
                final NaviSetting.NaviPathMode pathMode = mNaviSetting.getNaviPathMode();
                final int naviMode = getNaviShow();
                NaviSetting.DayNightMode realDayNightMode = NaviSetting.DayNightMode.values()[naviMode];
                if (naviMode != NaviSetting.DayNightMode.AUTO.ordinal()) {
                    setNaviShow(naviMode);
                }
                final NaviSetting.DayNightMode dayNightMode = realDayNightMode;

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        FlyLog.d(TAG, "playState: " + playState);
                        if (playState) {
                            if (level != null) {
                                FlyLog.d(TAG, "level: " + level);
                                switch (level) {
                                    case SIMPLE:
                                        mConentTv.setText(R.string.navi_content_simple);
                                        mContentLv.setItemChecked(1, true);
                                        break;
                                    case STANDARD:
                                        mConentTv.setText(R.string.navi_content_standard);
                                        mContentLv.setItemChecked(0, true);
                                        break;
                                    case FULL:
                                        mConentTv.setText(R.string.navi_content_full);
                                        mContentLv.setItemChecked(2, true);
                                        break;
                                }
                            } else {
                                FlyLog.e(TAG, "level is null");
                                mConentTv.setText(R.string.navi_content_standard);
                                mContentLv.setItemChecked(0, true);
                            }
                        } else {
                            mConentTv.setText(R.string.navi_content_none);
                            mContentLv.setItemChecked(3, true);
                        }

                        if (dayNightMode != null) {
                            FlyLog.d(TAG, "dayNightMode: " + dayNightMode);
                            switch (dayNightMode) {
                                case DAY:
                                    mMapShowTv.setText(R.string.navi_map_show_day);
                                    mMapShowLv.setItemChecked(1, true);
                                    break;
                                case NIGHT:
                                    mMapShowTv.setText(R.string.navi_map_show_night);
                                    mMapShowLv.setItemChecked(2, true);
                                    break;
                                case AUTO:
                                    mMapShowTv.setText(R.string.navi_map_show_auto);
                                    mMapShowLv.setItemChecked(0, true);
                                    break;
                            }
                        } else {
                            FlyLog.e(TAG, "dayNightMode is null");
                            mMapShowTv.setText(R.string.navi_map_show_auto);
                            mMapShowLv.setItemChecked(0, true);
                        }

                        if (pathMode != null) {
                            FlyLog.d(TAG, "pathMode: " + pathMode);
                            switch (pathMode) {
                                case DISTANCE:
                                    mNaviModeTv.setText(R.string.navi_navi_mode_distance);
                                    mNaviModeLv.setItemChecked(2, true);
                                    break;
                                case SUGGEST:
                                    mNaviModeTv.setText(R.string.navi_navi_mode_suggest);
                                    mNaviModeLv.setItemChecked(0, true);
                                    break;
                                case ECONOMIC:
                                    mNaviModeTv.setText(R.string.navi_navi_mode_economic);
                                    mNaviModeLv.setItemChecked(3, true);
                                    break;
                                case HIGHWAY:
                                    mNaviModeTv.setText(R.string.navi_navi_mode_highway);
                                    mNaviModeLv.setItemChecked(1, true);
                                    break;
                            }
                        } else {
                            FlyLog.e(TAG, "pathMode is null");
                            mNaviModeTv.setText(R.string.navi_navi_mode_suggest);
                            mNaviModeLv.setItemChecked(0, true);
                        }

                    }
                });
            }
        }).start();
    }

    @Override
    public void processCommandline(String cmd) {

    }

    @Override
    public String[] getCommands() {
        return new String[0];
    }

    private void initContentView(View v) {
        mConentTv = (TextView) v.findViewById(R.id.navi_content_value_tv);
        mContentDesTv = (TextView) v.findViewById(R.id.navi_content_desc_tv);
        mContentLv = (InnerListView) v.findViewById(R.id.navi_content_listview);
        CommonRadioGroupAdpt contentListAdpt = new CommonRadioGroupAdpt(getActivity());
        contentListAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_content_standard), getString(R.string.navi_content_standard_desc), R.string.navi_content_standard));
        contentListAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_content_simple), getString(R.string.navi_content_simple_desc), R.string.navi_content_simple));
        contentListAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_content_full), getString(R.string.navi_content_full_desc), R.string.navi_content_full));
        contentListAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_content_none), getString(R.string.navi_content_none_desc), R.string.navi_content_none));
        mContentLv.setAdapter(contentListAdpt);
        mContentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mConentTv.setText(getString((int) id));
                FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.navi) + "-" + getString(R.string.navi_content), getString((int) id));
                switch ((int) id) {
                    case R.string.navi_content_standard:
                        mNaviSetting.setContentPlayState(true);
                        mNaviSetting.setPlayedContentLevel(NaviSetting.PlayedContentLevel.STANDARD);
                        break;
                    case R.string.navi_content_simple:
                        mNaviSetting.setContentPlayState(true);
                        mNaviSetting.setPlayedContentLevel(NaviSetting.PlayedContentLevel.SIMPLE);
                        break;
                    case R.string.navi_content_full:
                        mNaviSetting.setContentPlayState(true);
                        mNaviSetting.setPlayedContentLevel(NaviSetting.PlayedContentLevel.FULL);
                        break;
                    case R.string.navi_content_none:
                        mNaviSetting.setContentPlayState(false);
                        break;
                }
            }
        });
    }


    private void initMapShowView(View v) {
        mMapShowTv = (TextView) v.findViewById(R.id.navi_map_show_value_tv);
        mMapShowDesTv = (TextView) v.findViewById(R.id.navi_map_show_desc_tv);
        mMapShowLv = (InnerListView) v.findViewById(R.id.navi_map_show_listview);
        CommonRadioGroupAdpt listAdpt = new CommonRadioGroupAdpt(getActivity());
        listAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_map_show_auto), R.string.navi_map_show_auto));
        listAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_map_show_day), R.string.navi_map_show_day));
        listAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_map_show_night), R.string.navi_map_show_night));
        mMapShowLv.setAdapter(listAdpt);
        mMapShowLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.navi) + "-" + getString(R.string.navi_map_show), getString((int) id));
                mMapShowTv.setText(getString((int) id));
                switch ((int) id) {
                    case R.string.navi_map_show_auto:
                        setNaviShow(0);
                        //mNaviSetting.setDayNightMode(NaviSetting.DayNightMode.AUTO);
                        break;
                    case R.string.navi_map_show_day:
                        setNaviShow(1);
                        mNaviSetting.setDayNightMode(NaviSetting.DayNightMode.DAY);
                        break;
                    case R.string.navi_map_show_night:
                        setNaviShow(2);
                        mNaviSetting.setDayNightMode(NaviSetting.DayNightMode.NIGHT);
                        break;
                }
            }
        });

    }


    private void initNaviModeView(View v) {
        mNaviModeTv = (TextView) v.findViewById(R.id.navi_navi_mode_value_tv);
        mNaviModeDesTv = (TextView) v.findViewById(R.id.navi_navi_mode_desc_tv);
        mNaviModeLv = (InnerListView) v.findViewById(R.id.navi_navi_mode_listview);
        CommonRadioGroupAdpt listAdpt = new CommonRadioGroupAdpt(getActivity());
        listAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_navi_mode_suggest), getString(R.string.navi_navi_mode_suggest_desc), R.string.navi_navi_mode_suggest));
        listAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_navi_mode_highway), R.string.navi_navi_mode_highway));
        listAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_navi_mode_distance), R.string.navi_navi_mode_distance));
        listAdpt.addItem(new CommonRadioButtonBean(getString(R.string.navi_navi_mode_economic), R.string.navi_navi_mode_economic));
        mNaviModeLv.setAdapter(listAdpt);
        mNaviModeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.navi) + "-" + getString(R.string.navi_navi_mode), getString((int) id));
                mNaviModeTv.setText(getString((int) id));
                switch ((int) id) {
                    case R.string.navi_navi_mode_distance:
                        mNaviSetting.setNaviPathMode(NaviSetting.NaviPathMode.DISTANCE);
                        break;
                    case R.string.navi_navi_mode_highway:
                        mNaviSetting.setNaviPathMode(NaviSetting.NaviPathMode.HIGHWAY);
                        break;
                    case R.string.navi_navi_mode_economic:
                        mNaviSetting.setNaviPathMode(NaviSetting.NaviPathMode.ECONOMIC);
                        break;
                    case R.string.navi_navi_mode_suggest:
                        mNaviSetting.setNaviPathMode(NaviSetting.NaviPathMode.SUGGEST);
                        break;
                }
            }
        });

    }

    private int getNaviShow() {
        return SystemProperties.getInt(PROPERTY_MAPAUTOMODE, 0);
    }

    private void setNaviShow(int mode) {
        if (mode != getNaviShow()) {
            Intent i = new Intent("autofly.intent.action.NAVI_SHOW_SET");
            i.putExtra("NAVI_SHOW", mode);
            getActivity().sendBroadcast(i);
        }
    }
}
